<template>
  <view>
    <!-- 搜索  吸顶效果-->
    <view class="search-box">
      <my-search :bgc="'skyblue'" :radius="18" @searchHandle="searchHandle"></my-search>
    </view>
    <!-- 轮播图 -->
    <swiper class="swiper-list" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000"
      :circular="true">
      <swiper-item v-for="(item, index) in swiperList" :key="index">
        <navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id">
          <image :src="item.image_src"></image>
        </navigator>>
      </swiper-item>
    </swiper>

    <!-- 导航列表 -->
    <view class="nav-list">
      <view class="nav-item" v-for="(item, index) in navList" :key="index" @click="navClickHandler(item)">
        <image class="nav-img" :src="item.image_src"></image>
      </view>
    </view>

    <!-- 楼层 -->
    <view class="floor-list">
      <view v-for="(item, index) in floorList" :key="index">
        <!-- 楼层标题 -->
        <image class="floor-title" :src="item.floor_title.image_src"></image>
        <!-- 楼层图片区域 -->
        <view class="floor-img">
          <!-- 左侧盒子 -->
          <navigator class="left-img" :url="item.product_list[0].url">
            <image :src="item.product_list[0].image_src" :style="{width: item.product_list[0].image_width + 'rpx'}">
            </image>
          </navigator>
          <!-- 右侧盒子 -->
          <view class="right-img-box">
            <navigator class="right-img" v-for="(imgItem, imgIndex) in item.product_list" :key="imgIndex"
              :url="imgItem.url">
              <view v-if="imgIndex !== 0">
                <image :src="imgItem.image_src" :style="{width: imgItem.image_width + 'rpx'}" mode="widthFix"></image>
              </view>
            </navigator>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  // 导入封装的 mixin 模块
  import cartBadge from '@/mixins/tabbar-badge.js'
  export default {
    // 将 badgeMix 混入到当前的页面中进行使用
    mixins: [cartBadge],
    data() {
      return {
        swiperList: [], // 轮播图列表数据
        navList: [], // 导航列表数据
        floorList: [], // 楼层列表数据
      };
    },

    onLoad() {
      this.getSwiperList()
      this.getNavList()
      this.getFloorList()
    },

    methods: {
      // 获取轮播图列表数据
      async getSwiperList() {
        // 解构赋值-将返回的data数据重新赋值给res
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/swiperdata')
        // console.log(res)
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        this.swiperList = res.message
        uni.$showMsg(res.meta.msg)
      },

      // 获取导航列表数据 
      async getNavList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/catitems')
        // console.log(res)
        if (res.meta.status !== 200) return uni.$showMsg()
        this.navList = res.message
      },

      // 点击商品分类
      navClickHandler(item) {
        // console.log(item)
        if (item.name === '分类') {
          uni.switchTab({
            url: '/pages/category/category'
          })
        }
      },

      async getFloorList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/floordata')
        // console.log(res)
        if (res.meta.status !== 200) return uni.$showMsg()
        res.message.forEach(floorItem => {
          floorItem.product_list.forEach(item => {
            item.url = '/subpkg/goods_list/goods_list?' + item.navigator_url.split('?')[1]
          })
        })
        this.floorList = res.message
      },

      // 点击搜索框进行跳转
      searchHandle() {
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      }
    }
  }
</script>

<style lang="scss">
  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
  }

  .swiper-list {
    height: 330rpx;

    .swiper-item,
    image {
      width: 100%;
      height: 100%;
    }
  }

  .nav-list {
    display: flex;
    justify-content: space-around;
    margin: 15px 0;

    .nav-item {
      .nav-img {
        width: 128rpx;
        height: 180rpx;
      }
    }
  }

  .floor-title {
    width: 100%;
    height: 60rpx;
  }

  .floor-img {
    display: flex;
    padding-left: 10rpx;


    .right-img-box {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-around;
    }

  }
</style>
